﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace Shared.DataBaseConfig
{
    /// <summary>
    /// 链接字符串管理
    /// </summary>
    public class DBConnectionManage
    {
        private static DBConnectionManage instance;
        private static object syncRoot = new Object();
        private static DBConnectionConfig m_DBConnectionConfig = null;
        public DBConnectionConfig DBConnectionConfig
        {
            get
            {
                if (null == m_DBConnectionConfig)
                {
                    throw new Exception("DBConnection Config not init");
                }
                return m_DBConnectionConfig;
            }
        }
        /// <summary>
        /// 单例模式运行
        /// </summary>
        public static DBConnectionManage Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new DBConnectionManage();
                        }
                    }
                }
                return instance;
            }
        }
        /// <summary>
        /// 初始化数据库链接字符串
        /// </summary>
        /// <param name="configuration"></param>
        public void InitConnectionConfig(IConfiguration configuration)
        {
            if (null == m_DBConnectionConfig)
            {
                lock (syncRoot)
                {
                    if (null == m_DBConnectionConfig)
                    {
                        m_DBConnectionConfig = configuration.Get<Shared.DataBaseConfig.DBConnectionConfig>();
                        if (null == m_DBConnectionConfig)
                        {
                            throw new Exception("DBConnection Config Error");
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 数据库链接配置
    /// </summary>
    public class DBConnectionConfig
    {
        /// <summary>
        /// 数据库链接配置集合
        /// </summary>
        public List<DBConnection> DBConnections { get; set; } = new List<DBConnection>();

        /// <summary>
        /// 根据配置名称获取数据库链接配置对象
        /// </summary>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        public DBConnection GetConnection(string connectionName)
        {
            return DBConnections.Find(t => string.Equals(t.ConnectionName, connectionName, StringComparison.OrdinalIgnoreCase));
        }
    }
    /// <summary>
    /// 数据库链接配置
    /// </summary>
    public class DBConnection
    {
        /// <summary>
        /// 链接配置名称
        /// Defalut
        /// </summary>
        public string ConnectionName { get; set; }

        /// <summary>
        /// 数据库类型
        /// MYSQL,MSSQL,ORACLE,SQLITE...
        /// </summary>
        public string DatabaseType { get; set; }
        /// <summary>
        /// 是否开启日志审计功能
        /// </summary>
        public bool SqlAudit { get; set; }

        /// <summary>
        /// 主库配置数据库链接
        /// </summary>
        public string Master { get; set; }

        /// <summary>
        /// 从库数据库链接配置集合
        /// </summary>
        public List<SlaveItem> Slave { get; set; } = new List<SlaveItem>();

        /// <summary>
        /// 根据链接配置名称获取数据库链接字符串
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="isMaster">是否获取主库配置</param>
        /// <returns></returns>
        public string GetConnectionString(bool isMaster = true)
        {
            if (isMaster)
            {
                return this.Master;
            }
            else
            {
                //这边应该增加权重判断处理.后续再新增

                return this.Slave[0].ConnectionString;
            }
        }
    }
    /// <summary>
    /// 从库配置项
    /// </summary>
    public class SlaveItem
    {
        /// <summary>
        /// 权重
        /// 满权重100
        /// </summary>
        public int Weight { get; set; } = 100;

        /// <summary>
        /// 从库链接字符串
        /// </summary>
        public string ConnectionString { get; set; }

    }
}
